/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.evangelion.core.controller;

import com.evangelion.core.bean.Alumno;
import com.evangelion.core.bean.Meses;
import com.evangelion.core.daoImpl.MesesDaoImpl;
import com.evangelion.core.utils.ManagerExcelReport;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author Kellycasma
 */
@ManagedBean
@ViewScoped
public class MesesController implements Serializable {

    MesesDaoImpl mdi = new MesesDaoImpl();
    private LazyDataModel<Meses> listMeses;
    DefaultStreamedContent dc;
    Meses mes;
    int idDesde;
    int idHasta;

    @PostConstruct
    public void init() {
        configDataModel();
        dc = null;
        mes = new Meses();
    }

    @SuppressWarnings("serial")
    public void configDataModel() {
        listMeses = new LazyDataModel<Meses>() {
            @Override
            public List<Meses> load(int start, int max, String arg2,
                    SortOrder arg3, Map<String, String> arg4) {
//                    if (valorBusqueda == null) {
//                        valorBusqueda = "";
//                    }
                return (List) mdi.searchByParam(Meses.class, "", start, max);
            }
        };
        listMeses.setRowCount(mdi.countPage(Meses.class, ""));
    }

    public LazyDataModel<Meses> getListMeses() {
        return listMeses;
    }

    public void setListMeses(LazyDataModel<Meses> listMeses) {
        this.listMeses = listMeses;
    }

    public void descargar(Meses mes) {
        this.mes = mes;
        this.mes.setEstado("Si");
        mdi.save(this.mes);
        File archivo = ManagerExcelReport.imprimir(this.mes);
        byte[] bytes = ManagerExcelReport.getBytes(archivo);
        InputStream stream = new ByteArrayInputStream(bytes);
        dc = new DefaultStreamedContent(stream, "application/xls", this.mes.getA() + ".xls");
        RequestContext.getCurrentInstance().update("idtablafacturareporte");

    }

    public void generar() {
        List<Meses> lis = (List) mdi.searchByParamdesde(Meses.class, "", idDesde, idHasta);
        for (Meses me : lis) {
            ManagerExcelReport.imprimir(me);
        }
    }

    public DefaultStreamedContent getDc() {
        return dc;
    }

    public void setDc(DefaultStreamedContent dc) {
        this.dc = dc;
    }

    public Meses getMes() {
        return mes;
    }

    public void setMes(Meses mes) {
        this.mes = mes;
    }

    public int getIdDesde() {
        return idDesde;
    }

    public void setIdDesde(int idDesde) {
        this.idDesde = idDesde;
    }

    public int getIdHasta() {
        return idHasta;
    }

    public void setIdHasta(int idHasta) {
        this.idHasta = idHasta;
    }
}
